#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include <fdb.h>
#include <fdb-helpers.h>

int main(int argc, char *argv[]) {
	char *data;
	size_t size;
	database_t *database;
	transaction_t *transaction;
	query_t *query;
	result_e result;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <database>\n", argv[0]);
		return 1;
	}

	database = database_open(argv[1]);
	if (database == NULL)
		return 1;

	transaction = transaction_begin(database, READ_COMMITTED);
	query = query_new(transaction);

	query_prepare(query, QUERY_SELECT, "data");
/*	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "Aaron\n", 7),
	                               NULL);
	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "Usenet\n", 7),
	                               NULL);
	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "deploy\n", 7),
	                               NULL);
	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "outbroken\n", 11),
	                               NULL);
	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "zygotes\n", 9),
	                               NULL);*/
	query_execute(query);
	while (1) {
		result = query_step(query);
		if (!result_ok(result))
			break;

		query_data_get(query, 0, &data, &size);
		printf("Row: %ld, ", *(int64_t*)data);
		query_data_get(query, 1, &data, &size);
		printf("'%s'\n", data);
	}
	query_finalize(query);

	query_destroy(query);

	transaction_commit(transaction);
	database_close(database);

	return 0;
}


